package dao;

import java.util.ArrayList;
import java.util.List;

import com.db4o.ObjectContainer;

import model.*;

public class MachineTypeDao {
	// private List<MachineType> machineTypes = new ArrayList<MachineType>();

	private static MachineTypeDao instance = null;

	public MachineTypeDao() {

	}

	/**
	 * This is a static method that return new machineTypeDao when instance is
	 * null
	 * 
	 * @author Osahon Idehen
	 */

	public static MachineTypeDao getInstance() {
		if (instance == null)
			instance = new MachineTypeDao();
		return instance;
	}

	// /**
	// * This adds a MachineTypes
	// *
	// * @author Osahon Idehen
	// */
	//
	// public void addMachineType(MachineType machineType) {
	// if (!machineTypes.contains(machineType)) {
	// machineTypes.add(machineType);
	// }
	// }
	//
	// /**
	// * This removes a MachineType
	// *
	// * @author Osahon Idehen
	// */
	//
	// public void removeMachineType(MachineType machineType) {
	// if (machineTypes.contains(machineTypes)) {
	// machineTypes.remove(machineType);
	// }
	// }
	//
	// /**
	// * This returns machineTypes from the list
	// *
	// * @author Osahon Idehen
	// */
	// public List<MachineType> getMachineTypes() {
	// return machineTypes;
	// }
	//
	// TODO extra methods

	/**
	 * Gets all the Machines from all the MachineTypes.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<Machine> getMachines() {
		List<Machine> machineList = new ArrayList<Machine>();
		for (MachineType mt : getMachineTypes()) {
			machineList.addAll(mt.getMachines());
		}
		return machineList;
	}

	/**
	 * Gets all the Reparations from all the Machines.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<Reparation> getReparations() {
		List<Reparation> repList = new ArrayList<Reparation>();
		for (Machine r : getMachines()) {
			System.out.println("aa: "+r.getReparations());
			repList.addAll(r.getReparations());
		}
		return repList;
	}

	/**
	 * Gets all the RepairTypes from all the Machines.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<RepairType> getRepairTypes() {
		List<RepairType> reptList = new ArrayList<RepairType>();
		for (MachineType mt : getMachineTypes()) {
			reptList.addAll(mt.getRepairTypes());
		}
		return reptList;
	}

	// TODO extra methods

	// TODO DB4O methods-start
	/**
	 * Link to the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	private ObjectContainer db = DaoManager.getInstance().getObjectContainer();

	/**
	 * Adds a MachineType to the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void addMachineType(MachineType machineType) {
		db.set(machineType);
		db.commit();
	}

	/**
	 * Removes a MachineType from the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void removeMachineType(MachineType machineType) {
		db.delete(machineType);
		db.commit();
	}

	/**
	 * Returns a List of MachineTypes from the database.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<MachineType> getMachineTypes() {
		return db.get(MachineType.class);
	}

	// TODO DB4O methods-end
}